package com.example.album;

import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;
import java.util.Map;
import java.util.HashMap;

@Controller
public class UserManagementHandler {
    @Autowired
    private UserManagementService ums;
    @Autowired
    private HttpSession session;
    @PostMapping("/logincheck")
    @ResponseBody
    public Map<String, Object> loginCheck(@RequestBody Map<String, String> payload) {
        String userid = payload.get("userid");
        String password = payload.get("password");
        Map<String, Object> result = new HashMap<>();
        result = ums.login(userid, password);
        return result;
    }
    @RequestMapping("/register")
    @ResponseBody
    public Map<String, Object> register(@RequestBody Map<String, String> payload) {
        String username = payload.get("username");
        String password = payload.get("password");
        String newUserId = ums.register(username, password);
        Map<String, Object> result = new HashMap<>();
        if (newUserId != null) {
            session.setAttribute("userid", newUserId);
            result.put("success", true);
            result.put("userid", newUserId);
        } else {
            result.put("success", false);
        }
        return result;
    }
    @RequestMapping("/getuserinfo")
    @ResponseBody
    public Map<String, Object> userInfo() {
        String userid = (String) session.getAttribute("userid");
        Map<String, Object> result = new HashMap<>();
        User user = ums.getUserById(userid);
        result.put("userid", user.getUserId());
        result.put("username", user.getUsername());
        result.put("role", user.getRole());
        result.put("phone", user.getPhoneNumber());
        result.put("status", user.getStatus());
        return result;
    }
    @RequestMapping("/logout")
    @ResponseBody
    public Map<String, Object> logout() {
        session.invalidate();
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }
    @RequestMapping("/updateuserinfo")
    @ResponseBody
    public Map<String, Object> updateUserInfo(@RequestBody Map<String, String> payload) {
        String userid = (String) session.getAttribute("userid");
        String newUsername = payload.get("username");
        String newPassword = payload.get("password");
        String newPhone = payload.get("phone");
        ums.updateUserInfo(userid, newUsername, newPassword, newPhone);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }
    @RequestMapping("/canceluser")
    @ResponseBody
    public Map<String, Object> cancelUser() {
        String userid = (String) session.getAttribute("userid");
        //逻辑删除用户
        ums.cancelUser(userid);
        session.invalidate();
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        return result;
    }

    @GetMapping("/getuserstats")
    @ResponseBody
    public Map<String, Object> getUserStats() {
        String userId = (String) session.getAttribute("userid");
        Map<String, Object> result = new HashMap<>();
        result= ums.getUserStats(userId);

        return result;
    }

    @GetMapping("/getalluserslist")
    @ResponseBody
    public Map<String, Object> getAllUsersList() {
        Map<String, Object> result = new HashMap<>();
        result = ums.getAllUsersList();
        return result;
    }
    

}
